package com.cjack.frenchexcel.util;

public class StringUtils {

    /**
     * 去掉字符串末尾字符串
     *
     * @param s      字符串
     * @param remove 末尾字符串
     * @return
     */
    public static String removeLast(String s, String remove) {
        if (s == null || s.isEmpty()) {
            return "";
        }
        if (remove == null || remove.isEmpty() || !s.contains(remove)) {
            return s;
        }
        if (s.equals(remove)) {
            return "";
        }
        // 要移除字符串长度
        int lengthR = remove.length();
        // 原字符串长度
        int lengthS = s.length();
        if (lengthR > lengthS) {
            return s;
        }
        // 末尾字符串
        String substring = s.substring(lengthS - lengthR, lengthS);
        if (substring.equals(remove)) {
            String substringR = s.substring(0, lengthS - lengthR);
            return substringR;
        }
        return s;
    }

    /**
     * 多个空格相邻保留一个空格
     *
     * @param str 字符串
     * @return
     */
    public static String leaveOneSpace(String str) {
        str = str.replaceAll(" +", " ");
        return str;
    }

    /**
     * 小写转大写字母
     *
     * @param str 字符串
     * @return
     */
    public static String toUpperCase(String str) {
        StringBuffer sb = new StringBuffer();
        if (str != null) {
            for (int i = 0; i < str.length(); i++) {
                char c = str.charAt(i);
                if (Character.isLowerCase(c) && c > 96 && c < 123) {
                    sb.append(Character.toUpperCase(c));
                } else {
                    sb.append(c);
                }
            }
        }
        return sb.toString();
    }

    /**
     * 大写转小写字母
     *
     * @param str 字符串
     * @return
     */
    public static String toLowerCase(String str) {
        StringBuffer sb = new StringBuffer();
        if (str != null) {
            for (int i = 0; i < str.length(); i++) {
                char c = str.charAt(i);
                if (Character.isUpperCase(c) && c > 64 && c < 91) {
                    sb.append(Character.toLowerCase(c));
                } else {
                    sb.append(c);
                }
            }
        }
        return sb.toString();
    }

    /**
     * 大写转小写，小写转大写
     *
     * @param str 字符串
     * @return
     */
    public static String exChange(String str) {
        StringBuffer sb = new StringBuffer();
        if (str != null) {
            for (int i = 0; i < str.length(); i++) {
                char c = str.charAt(i);
                if (Character.isUpperCase(c) && c > 64 && c < 91) {
                    sb.append(Character.toLowerCase(c));
                } else if (Character.isLowerCase(c) && c > 96 && c < 123) {
                    sb.append(Character.toUpperCase(c));
                } else {
                    sb.append(c);
                }
            }
        }
        return sb.toString();
    }


}
